<?php

namespace App\Http\Controllers\Front;

use App\Model\UserMarketing;
use App\Model\UserPopularity;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserMarketingController extends Controller
{

    /**
     * @SWG\Get(
     *     path="/front/user-marketing",
     *     tags={"个人网页"},
     *     summary="营销网页",
     *     description="营销网页",
     *     produces={"application/json"},
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *          @SWG\Schema(
     *              type="array",
     *              @SWG\Items(
     *                  @SWG\Property( property="id", type="integer",description="营销网页id"),
     *                   @SWG\Property(property="title", type="string", description="标题"),
     *                   @SWG\Property(property="pop", type="integer", description="浏览量"),
     *                   @SWG\Property(property="contents", type="string", description="内容"),
     *                   @SWG\Property(property="share", type="integer", description="分享量"),
     *                   @SWG\Property(property="cover", type="string", description="封面"),
     *                   @SWG\Property(property="creasted_at", type="string", format="date", description="时间"),
     *              )
     *          )
     *      )
     * )
     */
    public function index(Request $request)
    {
        $user_id =  $request->user()->id;
        $user_marketing = UserMarketing::select('id','title', 'cover', 'contents', 'pop', 'share', 'created_at')->where(["user_id"=> $user_id, 'is_deleted' => 0])->orderBy("created_at", "desc")->paginate(request('limit', 10));
        $items = $user_marketing->items();
        $data = [];
        foreach ($items as $item){
            $tmp = [
                'id' => $item->id,
                'title' => $item->title,
                'cover' => $item->cover,
                'contents' => $item->contents,
                'pop' => $item->pop,
                'share' => $item->share,
                'created_at' => $item->created_at->format('Y-m-d H:i:s')
            ];
            foreach (json_decode($item->contents)->content as $content) {
                if ($content->type == 'img') {
                    $tmp['cover'] = $content->src;
                    break;
                }
            }
            $data[] = $tmp;
        }
        $user_marketing = $user_marketing->toArray();
        $user_marketing['data'] = $data;

        return $this->renderJson($user_marketing);
    }

    /**
     * 获取单个营销网页
     * @param $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $user_marking = UserMarketing::where("id", $id)->first();
        if ($user_marking){
        	//记录人气
			UserPopularity::add(UserPopularity::WEB_PAGE,$user_marking->user_id,$id);
            return $this->renderJson($user_marking);
        }
        return $this->error();
    }

    public function store(Request $request)
    {
        $this->validate($request,[
            'title' => 'required',
            'contents' => 'required'
        ],[
            'required' => ':attribute不能为空'
        ],[
            'title' => '标题',
            'contents' => '内容',
        ]);

        $user_marking = new UserMarketing();

        if (!$id = $user_marking->add($request)){
            return $this->error("添加失败！");
        }
        return $this->renderJson(['id'=> $id]);
    }
    /**
     * 修改
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request ,$id)
    {
        $this->validate($request,[
            'title' => 'required',
            'contents' => 'required'
        ],[
            'required' => ':attribute不能为空'
        ],[
            'title' => '标题',
            'contents' => '内容',
        ]);

        $user_marking = UserMarketing::where("id", $id)->first();
        
       if (! $id = $user_marking->add($request)){
            return $this->error("添加失败！");
       }
        return $this->renderJson(['id' => $id]);

    }

    /**
     * @SWG\Delete(
     *     path="/front/user-marketing/{id}",
     *     tags={"个人网页"},
     *     summary="删除营销网页",
     *     description="删除营销网页",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", required=true, type="string", description="id"),
     *     @SWG\Response(
     *          response="200",
     *          description="删除成功"
     *      )
     * )
     */
    public function destroy(Request $request,$id)
    {
        $user_makeing = UserMarketing::where("id", $id)->first();
        $user_makeing->delete();
        return $this->success("删除成功！");
    }

    /**
     * @SWG\Get(
     *     path="/front/marketing/byuserid",
     *     tags={"个人网页"},
     *     summary="根据用户id获取营销网页列表",
     *     description="根据用户id获取营销网页列表",
     *     produces={"application/json"},
     *     @SWG\Parameter(name="user_id", in="query", description="用户id", type="integer"),
     *     @SWG\Parameter(name="page", in="query", description="页码", type="integer"),
     *     @SWG\Parameter(name="limit", in="query", description="步长", type="integer"),
     *     @SWG\Response(
     *         response="200",
     *         description="个人网页列表",
     *         @SWG\Schema(
     *              type="array",
     *              @SWG\Items(
     *                   @SWG\Property(property="id", type="integer", description="营销网页id"),
     *                   @SWG\Property(property="title", type="string", description="标题"),
     *                   @SWG\Property(property="popularity", type="integer", description="浏览量"),
     *                   @SWG\Property(property="contents", type="string", description="内容")
     *              )
     *          )
     *     )
     * )
     */
    public function getListByUserId()
    {
        $user_id = request('user_id');
        $user_marketing = UserMarketing::select(['id', 'title', "cover", 'contents', 'pop as popularity', 'created_at', 'share'])->where('user_id', $user_id)->where('is_deleted', 0)->paginate(request('limit', 10));
        $items = $user_marketing->items();
        $data = [];
        foreach ($items as $item){
            $tmp = [
                'id' => $item->id,
                'title' => $item->title,
                'cover' => $item->cover,
                'contents' => $item->contents,
                'popularity' => $item->popularity,
                'share' => $item->share,
                'created_at' => $item->created_at->format('Y-m-d H:i:s')
            ];
            foreach (json_decode($item->contents)->content as $content) {
                if ($content->type == 'img') {
                    $tmp['cover'] = $content->src;
                    break;
                }
            }
            $data[] = $tmp;
        }
        $user_marketing = $user_marketing->toArray();
        $user_marketing['data'] = $data;
        return $this->renderJson($user_marketing);
    }


}
